package com.loong.android.http;

import android.content.Context;

import com.loong.android.LonApp;
import com.loong.android.http.config.ConfigConst;
import com.loong.android.http.config.HttpConfig;
import com.loong.android.http.request.StringDeleteRequest;
import com.loong.android.http.request.FileGetRequest;
import com.loong.android.http.request.StringGetRequest;
import com.loong.android.http.request.StringPostRequest;
import com.loong.android.http.request.StringPutRequest;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.cookie.CookieJarImpl;
import com.lzy.okgo.cookie.store.DBCookieStore;
import com.lzy.okgo.cookie.store.MemoryCookieStore;
import com.lzy.okgo.cookie.store.SPCookieStore;
import com.lzy.okgo.interceptor.HttpLoggingInterceptor;

import java.util.concurrent.TimeUnit;

import okhttp3.OkHttpClient;

/**
 * 网络请求组件。支持get、post、put、delete四种常用请求方式；支持文件上传和下载。
 * 需要手动添加权限：android.permission.INTERNET
 */
public class Http {

    /**
     * 初始化
     * @param config 初始化配置
     */
    public static void init(HttpConfig config) {
        Context context = config.getContext();
        OkHttpClient.Builder builder = new OkHttpClient.Builder();
        //log相关
        if(config.isDebugEnable()) {
            HttpLoggingInterceptor loggingInterceptor = new HttpLoggingInterceptor("OkHttp");
            //log打印级别，决定了log显示的详细程度
            loggingInterceptor.setPrintLevel(ConfigConst.LOG_LEVEL[config.getDebugLogType()]);
            //log颜色级别，决定了log在控制台显示的颜色
            loggingInterceptor.setColorLevel(ConfigConst.COLOR_LEVEL[config.getDebugLogColorType()]);
            builder.addInterceptor(loggingInterceptor);
        }
        //全局的读取超时时间
        builder.readTimeout(config.getReadTimeout(), TimeUnit.MILLISECONDS);
        //全局的写入超时时间
        builder.writeTimeout(config.getWriteTimeout(), TimeUnit.MILLISECONDS);
        //全局的连接超时时间
        builder.connectTimeout(config.getConnectTimeout(), TimeUnit.MILLISECONDS);

        switch (config.getCookieMode()) {
            case HttpConfig.CookieMode.SP:
                builder.cookieJar(new CookieJarImpl(new SPCookieStore(context)));
                break;
            case HttpConfig.CookieMode.DB:
                builder.cookieJar(new CookieJarImpl(new DBCookieStore(context)));
                break;
            case HttpConfig.CookieMode.MEMORY:
                builder.cookieJar(new CookieJarImpl(new MemoryCookieStore()));
                break;
        }

        OkGo okGo = OkGo.getInstance().init(LonApp.getInstance())
                .setOkHttpClient(builder.build())
                //全局统一缓存模式，默认不使用缓存
                .setCacheMode(ConfigConst.CACHE_MODE[config.getCacheMode()])
                //全局统一缓存时间，默认永不过期
                .setCacheTime(config.getCacheTime())
                //全局统一超时重连次数，默认为三次
                .setRetryCount(config.getRetryCount());

        if(config.getHttpHeaders() != null) okGo.addCommonHeaders(config.getHttpHeaders());
        if(config.getHttpParams() != null) okGo.addCommonParams(config.getHttpParams());
    }

    /**
     * 简版的初始化
     *
     * @param context 上下文
     */
    public static void init(Context context) {
        init(new HttpConfig(context));
    }

    /**
     * 创建get请求
     *
     * @param url 接口地址
     */
    public static StringGetRequest get(String url) {
        return new StringGetRequest(url);
    }

    /**
     * 创建post请求
     *
     * @param url 接口地址
     */
    public static StringPostRequest post(String url) {
        return new StringPostRequest(url);
    }

    /**
     * 创建put请求
     *
     * @param url 接口地址
     */
    public static StringPutRequest put(String url) {
        return new StringPutRequest(url);
    }

    /**
     * 创建delete请求
     *
     * @param url 接口地址
     */
    public static StringDeleteRequest delete(String url) {
        return new StringDeleteRequest(url);
    }

    /**
     * 创建文件get请求(文件下载)
     *
     * @param url 文件下载地址
     */
    public static FileGetRequest fileGet(String url) {
        return new FileGetRequest(url);
    }

    /**
     * 取消请求
     * @param tag 请求标签数据
     */
    public static void cancel(Object tag) {
        OkGo.getInstance().cancelTag(tag);
    }

    /**
     * 取消所有请求
     */
    public static void cancelAll() {
        OkGo.getInstance().cancelAll();
    }
}
